#ifndef __demo_gps_kenny_kfjiweiiur89u83r33iwuee3e__
#define __demo_gps_kenny_kfjiweiiur89u83r33iwuee3e__

#include <hude/aio/context.hpp>

#include "plugs/virt-gps/client-gps.hpp"

#include "test/test.hpp"

namespace test
{
    using namespace ha;

    //--------------------------------------------------------------------------------
    struct demo_gps_t : public hude::test::case_t
    {
        enum : uint16_t { emCOUNT = 2U };

        using gps_t = ha::gps_client_t;

        hude::aio::aio_t<> aio_;
        bool               ret_;

        // 测试过程
        bool run() override;

        bool demo();
        void stop();
    };

    //--------------------------------------------------------------------------------
    inline bool demo_gps_t::run()
    {
        aio_ = hude::aio::context_t<>::aio();

        auto ret = aio_->run( [ = ]() {
            std::thread{ [=] { ret_ = demo(); stop(); } }.detach();
        } );

        return ret == 0 && ret_;
    }

    //--------------------------------------------------------------------------------
    inline bool demo_gps_t::demo()
    {
        auto print = []( const gps_t& gps, const char* item )
        {
            auto info = gps[ item ];
        
            if( info )
            {
                log_inf( "\t <GPS> %s: %s", item, (*info).c_str() );
            } else {
                log_war( "\t <GPS> %s failed.", item );
            }

            return !!info;
        };

        gps_t gps( aio_ );

        if( ! gps.init( "/tmp/dev/ttyGPS", 115200U ) )
        {
            log_err( "ERROR: init gps: \"/tmp/dev/ttyGPS\"." );

            return false;
        }

        std::this_thread::sleep_for( std::chrono::seconds( 20U ) );

        bool ret = true;

        ret = print( gps, "GNRMC" ) && ret; // "$GNRMC,065004.00,A,2309.41232,N,11331.31714,E,0.024,170720,A63",
        ret = print( gps, "GNVTG" ) && ret; // "$GNVTG,T,M,0.024,N,0.044,K,A3B",
        ret = print( gps, "GNGGA" ) && ret; // "$GNGGA,065004.00,2309.41232,N,11331.31714,E,1,09,0.77,115.4,M,-4.8,M,53",
        ret = print( gps, "GNGSA" ) && ret; // "$GNGSA,A,3,12,06,25,17,02,05,19,13,09,1.71,0.77,1.531C",
        ret = print( gps, "GPGSV" ) && ret; // "$GPGSV,3,1,09,02,47,345,35,05,58,260,21,06,41,042,29,09,24,056,2971",
        ret = print( gps, "GPGSV" ) && ret; // "$GPGSV,3,2,09,12,34,280,23,13,14,182,13,17,35,130,38,19,53,118,3975",
        ret = print( gps, "GPGSV" ) && ret; // "$GPGSV,3,3,09,25,13,306,2042",
        ret = print( gps, "GNGLL" ) && ret; // "$GNGLL,2309.41232,N,11331.31714,E,065004.00,A,A7F"

        return ret;
    }

    //--------------------------------------------------------------------------------
    inline void demo_gps_t::stop()
    {
        this->aio_->cancel();
    }

    TEST_CASE_ADD( "gps", demo_gps_t, false );

} // namespace test

#endif // __demo_gps_kenny_kfjiweiiur89u83r33iwuee3e__




